using System;
using System.Collections; 
using UnityEngine;
using UnityEngine.SceneManagement;
using XFABManager;

namespace XFGameFramework.CommonSystem
{
    /// <summary>
    /// 场景加载的业务逻辑
    /// </summary>
    public class SceneController : Controller
    {

        /// <summary>
        /// 加载场景(资源模块名称是当前业务逻辑所在的模块)
        /// </summary>
        /// <param name="scene_name">场景名称</param>
        /// <param name="mode">加载模式</param>
        /// <param name="force_loading">强制加载(true:强制加载场景,false:如果场景已加载,则不再重新加载)</param>
        public void LoadScene(string scene_name, LoadSceneMode mode, bool force_loading = false) 
        {
            LoadScene(Module.ProjectName,scene_name,mode,force_loading);
        }

        /// <summary>
        /// 加载场景
        /// </summary>
        /// <param name="projectName">场景所在资源模块名</param>
        /// <param name="scene_name">场景名称</param>
        /// <param name="mode">加载模式</param>
        /// <param name="force_loading">强制加载(true:强制加载场景,false:如果场景已加载,则不再重新加载)</param>
        public void LoadScene(string projectName,string scene_name, LoadSceneMode mode, bool force_loading = false )
        {
            if (force_loading == false)
            {
                Scene scene = SceneManager.GetSceneByName(scene_name);
                if (scene.IsValid())
                {
                    return;
                }
            }

            AssetBundleManager.LoadScene(projectName, scene_name, mode); 
        }

        /// <summary>
        /// 异步加载场景(资源模块名称是当前业务逻辑所在的模块)
        /// </summary>
        /// <param name="scene_name">场景名称</param>
        /// <param name="mode">加载模式</param>
        /// <param name="force_loading">强制加载(true:强制加载场景,false:如果场景已加载,则不再重新加载)</param>
        /// <returns></returns>
        public LoadSceneRequest LoadSceneAsync(string scene_name, LoadSceneMode mode, bool force_loading = false) 
        { 
            return LoadSceneAsync(Module.ProjectName,scene_name, mode, force_loading);
        }

        /// <summary>
        /// 异步加载场景
        /// </summary>
        /// <param name="projectName">场景所在资源模块名</param>
        /// <param name="scene_name">场景名称</param>
        /// <param name="mode">加载模式</param>
        /// <param name="force_loading">强制加载(true:强制加载场景,false:如果场景已加载,则不再重新加载)</param>
        /// <returns></returns>
        public LoadSceneRequest LoadSceneAsync(string projectName,string scene_name, LoadSceneMode mode, bool force_loading = false) 
        {
            if (force_loading == false)
            {
                Scene scene = SceneManager.GetSceneByName(scene_name);
                if (scene.IsValid())
                {
                    LoadSceneRequest request = new LoadSceneRequest();
                    request.Abort(); // 中断
                    return request;
                }
            }

            return AssetBundleManager.LoadSceneAsynchrony(projectName, scene_name, mode);
        }
         
    }

}

